#pragma once

#include "Movies_admin.h"
#include "reservation_admin.h"
#include <iostream>

using namespace std;

movies_admin *movies_admin::instance=NULL;

movies_admin::movies_admin()
{
	movies_counter = 0;
	cds_counter=0;
};

movies_admin *movies_admin::get_movies_admin()
{
	if (instance==NULL) instance = new movies_admin();
	return instance;
};

bool movies_admin::search(string x)
{
	if(movies_counter > 0)
	{if(database.find(x)!=database.end())
	{ return true;}
	else return false;
	/*for(int i=0;i<movie_counter;i++)
	{
		if(x == movies[i].getname())
		{
			return true;
		}
		else 
			return false;
	}*/
	}
	else if(movies_counter == 0)
	{return false;}
};

void movies_admin::add_movie(string movie_name , int copies_no , string movie_details)
{
	bool a = search(movie_name);
	if(a == false)
	{
		database[movie_name] = movie_title (movie_name , copies_no , copies_no , movie_details);
		movies_counter++;

		for(int i=0;i<copies_no;i++)
		{
			movie_cd t(&database[movie_name]);
			cds.push_back(t);
			cds_counter++; // By Abdelrahman
		}

	}
	else {cout << "This movie is alraedy EXISTED!";}
};

void movies_admin::add_copies(string movie_name , int extra_no)
{
	movie_title A;
	A = database[movie_name];
	A.inc_copies(extra_no);
	database[movie_name] = A;
};

int movies_admin::get_copies(string movie_name)
{
	int a;
	movie_title M;
	M = database[movie_name];
	a = M.get_copies();
	return a;
};

void movies_admin::display()
{
	for(map<string , movie_title>::const_iterator it = database.begin(); it != database.end() ;++it)
	{
		cout <<it->first <<endl;
	}
};

void movies_admin::view_movie_details(string movie_name)
{
	database[movie_name].view();
};

void movies_admin::inc_copies_available(string name, int copies)
{
 database[name].inc_copies_available(copies);
};

void movies_admin::dec_copies_available(string name , int copies)
{
 database[name].dec_copies_available(copies);
};


// Abdelrahman

movie_cd *movies_admin::get_available_cd(string movie)
{
	for (int i=0; i<cds_counter;i++)
	{
		if(cds[i].get_movie_title()->get_name()==movie)
		{
			if(!cds[i].is_rented)
				return &cds[i];
		}
	}
};

bool movies_admin::check_availability(string name_)
{
	if (movies_admin::get_available_copies(name_)>0)
	{
		if ((reservation_admin::get_reservation_admin()->is_reserved(name_)) && (movies_admin::get_available_copies(name_)==1))
		{	return false;}
		return true;
	}

	else return false ;
};

int movies_admin::get_available_copies(string movie_name)
{
 return database[movie_name].get_copies_available();
}